using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Events;
using NCS.Library.Components.Security;
using NCS.Library;
using NCS.Library.Components;

namespace NCS.Module.CMS.Admin.WorkFlow
{
    public partial class EventList : AuthenticatedControl
    {
        private string curDateString = DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString();
        private string uploadPath = "/Modules/CMS/Upload/";
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUpdateStatus.Text = string.Empty;
        }
        private void CheckPath()
        {
            if (uploadPath == string.Empty)
            {
                return;
            }
            //uploadPath += CurrentUser.UserID + "/" + curDateString + "/";
            string avatarPath = uploadPath + "Avatar/";

            if (!Directory.Exists(Server.MapPath(uploadPath)))
            {
                Directory.CreateDirectory(Server.MapPath(uploadPath));
            }
            if (!Directory.Exists(Server.MapPath(avatarPath)))
            {
                Directory.CreateDirectory(Server.MapPath(avatarPath));
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            CheckPath();

            txtAvatar.fpUploadDir = uploadPath;
            LoadData();
        }     
        

        void LoadData()
        {
            dgEvents.DataSource = EventController.GetAll();
            dgEvents.DataBind();
        }
        protected void dgEvents_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView curData = (DataRowView)e.Item.DataItem;
                Button Button3 = (Button)e.Item.FindControl("Button3");
                Button3.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
                e.Item.Cells[3].Text = ConvertUtility.ToDateTime(e.Item.Cells[3].Text).ToString("dd-MM-yyyy");

                Image imgAvatar = (Image) e.Item.FindControl("imgAvatar");
                if (ConvertUtility.ToString(curData["Event_Avatar"]) != string.Empty)
                {
                    imgAvatar.ImageUrl = ConvertUtility.ToString(curData["Event_Avatar"]); // MultimediaUtility.GetEventAvatar(ConvertUtility.ToString(curData["Event_Avatar"]));
                }
                else
                {
                    imgAvatar.Visible = false;
                }
                DropDownList dropIndex = (DropDownList)e.Item.FindControl("dropIndex");
                MiscUtility.FillIndex(dropIndex, 40, ConvertUtility.ToInt32(curData["Indexs"]));
            }
        }

        protected void dgEvents_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (e.CommandName == "edit")
            {
                EventInfo info = EventController.GetInfo(Convert.ToInt32(e.Item.Cells[0].Text));
                txtID.Text = info.Event_ID.ToString();
                txtName.Text = info.Event_Name;
                txtDes.Text = info.Event_Description;
                txtAvatar.Text = info.Event_Avatar;
            }
            if (e.CommandName == "delete")
            {
                try
                {
                    EventController.Event_Delete(ConvertUtility.ToInt32(e.Item.Cells[0].Text));
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                    this.AddLog("CMS Event", " try delete event " + e.Item.Cells[0].Text + " success");
                }
                catch(Exception ex)
                {
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                    this.AddLog("CMS Event", " try delete event error: " + ex.Message);
                }
            }
            if (e.CommandName == "updatepriority")
            {
                foreach (DataGridItem item in dgEvents.Items)
                {
                    DropDownList dropIndex = (DropDownList)item.FindControl("dropIndex");
                    EventController.SetPriority(ConvertUtility.ToInt32(item.Cells[0].Text), ConvertUtility.ToInt32(dropIndex.SelectedValue));
                    this.AddLog("CMS Event", " try update priority of event " + e.Item.Cells[0].Text + " to "+ dropIndex.SelectedValue +" success");
                }
            }
            if (e.CommandName == "detail")
            {
                Response.Redirect(AppEnv.AdminUrlParams("cmseventdetail") + "&eventid=" + e.Item.Cells[0].Text);
            }
        }

        protected void btnUpdate_Click1(object sender, EventArgs e)
        {
            EventInfo info = EventController.GetInfo(ConvertUtility.ToInt32(txtID.Text));
            if (info == null) return;
            info.Event_Name = txtName.Text;
            info.Event_Description = txtDes.Text;
            info.Event_CreateDate = DateTime.Now;
            info.Event_Avatar =  txtAvatar.Text;
            info.PortalID = ConvertUtility.ToInt32(AppEnv.PortalId());
            //MultimediaUtility.SetAvatarThumbnail(Server.MapPath(txtAvatar.Text), Constants.ThumbListWidth, 0);
           
            try
            {
                EventController.Event_Update(info);
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                this.AddLog("CMS Event", " try update event " + info.Event_Name + " success");
            }
            catch(Exception ex)
            {
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                this.AddLog("CMS Event", " try update event error: " + ex.Message);
            }
            LoadData();
        }

        protected void btnAddNew_Click1(object sender, EventArgs e)
        {
            EventInfo info = new EventInfo();

            //MultimediaUtility.SetAvatarEventThumbnail(Server.MapPath(txtAvatar.Text), Constants.ThumbListWidth, 0);
            
            info.Event_Name = txtName.Text;
            info.Event_Description = txtDes.Text;
            info.Event_CreateDate = DateTime.Now;
            info.Event_Avatar =  txtAvatar.Text;
            info.Event_Lang = AppEnv.GetLanguage();
            info.PortalID = ConvertUtility.ToInt32(AppEnv.PortalId());
            
            try
            {
                txtID.Text = EventController.Event_Insert(info).ToString();
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                this.AddLog("CMS Event", " try add new event " + info.Event_Name + " success");
            }
            catch(Exception ex)
            {
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                this.AddLog("CMS Event", " try add new event error: " + ex.Message);
            }
            LoadData();
        }
    }
}